﻿using AutoMapper;
using SXFramWork.Core;
using SXFramWork.Model;
using SXFramWork.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SXFramWork.Service
{
    public class SysJobGroupService : ISysJobGroupService
    {
        private readonly ISysJobGroupRepository _repository;
        private readonly IMapper _mapper;
        public SysJobGroupService(ISysJobGroupRepository sysJobGroupRepository,IMapper mapper)
        {
            _mapper = mapper;
            _repository = sysJobGroupRepository;
        }

        public async Task<IApiResult> Add(JobGroupAddModel model)
        {
            var entity = _mapper.Map<SysJobGroupEntity>(model);
            entity.Id = GuidHelper.NewSequentialGuid(SequentialGuidType.SequentialAtEnd);
            entity.CreateTime = DateTime.Now;
            var res= await _repository.InsertAsync(entity);
            if (res.IsNull())
                return ApiResult.NotOk("添加失败");
            return ApiResult.Ok();
        }

        public async Task<IApiResult> Delete(string ids)
        {
           var res= await _repository.DeleteManyAsync(ids.Split(',').ToList(), "sysjobgroup");
            return ApiResult.Ok(res);
        }

        public async Task<IApiResult> Query(JobGroupQueryModel model)
        {
            var result = new PageResult<SysJobGroupEntity>
            {
                TotalCount = model.TotalCount,
                Rows = await _repository.Query(model)
            };
            return ApiResult.Ok(result);
        }
    }
}
